package com.jcode.skeleton.web;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.BindException;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ModelAttribute;

import com.google.common.base.Throwables;
import com.jcode.skeleton.exception.BizException;
import com.jcode.skeleton.model.ErrorCode;
import com.jcode.skeleton.model.ResponsePacket;
import com.jcode.skeleton.util.FastJsonUtil;

/**
 * 
 * @Desc
 *
 * @Author dreajay
 * @DateTime 2015-10-9 下午07:57:00
 *
 */
//@ControllerAdvice
public class BaseController {

//	protected HttpServletRequest request;
//	protected HttpServletResponse response;
	private static Logger logger = LoggerFactory.getLogger(BaseController.class);

	// 1)放置在方法的形参上：表示引用Model中的数据
	// 2)放置在方法上面：表示请求该类的每个Action前都会首先执行它，你可以将一些准备数据的操作放置在该方法里面。
//	@ModelAttribute
//	public void setContext(HttpServletRequest request, HttpServletResponse response) {
//		this.request = request;
//		this.response = response;
//	}

	public void responseJson(HttpServletResponse response, String json) throws IOException {
		response.setContentType("text/json;charset=UTF-8");
		response.getOutputStream().write(json.getBytes("UTF-8"));
	}

	public void responseJson(HttpServletResponse response, Object object) throws IOException {
		responseJson(response, FastJsonUtil.toJson(object));
	}

	/**
	 * BizException业务异常处理，返回JSON数据
	 */
	@ExceptionHandler(value = { BizException.class })
	public void handlerException(HttpServletRequest request, HttpServletResponse response, BizException e) throws IOException {
		logger.error("业务错误:errCode={},errMsg={}, exceptionStackTrace={}", e.getErrCode(), e.getMessage(), Throwables.getStackTraceAsString(e));
		responseJson(response, e.getPacket());
	}

	/**
	 * BindException校验异常处理，返回JSON数据
	 */
	@ExceptionHandler(value = { BindException.class })
	public void handlerException(HttpServletRequest request, HttpServletResponse response, BindException e) throws IOException {
		FieldError fieldError = e.getFieldError();
		logger.error("校验错误:ObjectName={}, errCode={}, errMsg={}, exceptionStackTrace={}", fieldError.getObjectName(), fieldError.getCode(), fieldError.getDefaultMessage(), Throwables.getStackTraceAsString(e));
		responseJson(response, new ResponsePacket(ErrorCode.SYS_9001, fieldError.getDefaultMessage()));
	}

	/**
	 * Exception异常处理，返回JSON数据
	 */
	@ExceptionHandler(value = { Exception.class })
	public void handlerException(HttpServletRequest request, HttpServletResponse response, Exception e) throws IOException {
		logger.error("未知错误:exceptionStackTrace={}", Throwables.getStackTraceAsString(e));
		responseJson(response, ResponsePacket.getResponsePacket(ErrorCode.SYS_9000, "系统错误,请联系系统管理员"));

	}
}
